<template>
  <!-- 概览 -->
  <div class="home-overview">
    <!-- 用户信息 -->
    <div class="user-meta">
      <div class="avatar">
        <img :src="$store.state.user.profile.avatar" @click='inputFile.click()'/>
        <input type="file" accept=".png, .jpg, .gif " style="display: none;" ref="inputFile">
      </div>
      <h4>{{$store.state.user.profile.account}}</h4>
    </div>
    <div class="item">
      <a href="javascript:;">
        <span class="iconfont icon-hy"></span>
        <p>会员中心</p>
      </a>
      <a href="javascript:;">
        <span class="iconfont icon-aq"></span>
        <p>安全设置</p>
      </a>
      <a href="javascript:;">
        <span class="iconfont icon-dw"></span>
        <p>地址管理</p>
      </a>
    </div>
  </div>
</template>
<script>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getEditAvatar } from '@/api/memberHome'
import { useStore } from 'vuex'
export default {
  name: 'MemberHomeOverview',
  setup(props) {
    const goods = {
      id: '1',
      name: '自煮火锅不排队 麦饭石不粘鸳鸯火锅',
      picture: 'https://yanxuan-item.nosdn.127.net/fcdcb840a0f5dd754bb8fd2157579012.jpg',
      desc: '清汤鲜香 红汤劲爽',
      price: '159.00'
    }
    // 修改头像
    const state = useStore()
    const inputFile = ref(null)
    const editAvatar = async (e) => {
      var files = e.target.files;
      if(files[0]){
        console.log('123')
        if(!/\.(jpg|gif|png)$/.test(e.target.value)){
          return ElMessage('格式错误')
        }
        var formData = new FormData()
        formData.append('file',files[0]);
        const { result } = await getEditAvatar(formData)
        state.commit('user/setUserAvatar',result)
        ElMessage.success('更改成功')
      }
    }
    onMounted(() => {
      inputFile.value.addEventListener('change',e=>{
        editAvatar(e)
      })
    })
    return { goods, editAvatar, inputFile }
  }
}
</script>
<style scoped lang="scss">
.home-overview {
  height: 132px;
  background: url(../../../../assets/images/center-bg.png) no-repeat center / cover;
  display: flex;
  .user-meta {
    flex: 1;
    display: flex;
    align-items: center;
    .avatar {
      width: 85px;
      height: 85px;
      border-radius: 50%;
      overflow: hidden;
      margin-left: 60px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    h4 {
      padding-left: 26px;
      font-size: 18px;
      font-weight: normal;
      color: white;
    }
  }
  .item {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-around;
    &:first-child {
      border-right: 1px solid #f4f4f4;
    }
    a {
      color: white;
      font-size: 16px;
      text-align: center;
      .iconfont {
        font-size: 32px;
      }
      p {
        line-height: 32px;
      }
    }
  }
}
</style>